import * as types from './historyMutationTypes';
import arrayUtil from '../../../common/arrayUtil';


/**
 * mutations
 * @type {{}}
 */
const mutations = {

  [types.SYS_FILE_UPLOAD_QUERY_TOKEN](state) {
  },
  [types.SYS_FILE_UPLOAD_SAVE_FILE_BEGIN](state) {
  },
  [types.SYS_FILE_UPLOAD_SAVE_FILE_SUC](state, {file}) {

    state.History.unshift(file);
    state.totalCount++;

  },
  [types.SYS_FILE_UPLOAD_UPDATE_SUC](state, {fileInfo}) {

    let file = state.History.find((item) => {
      return item.id === fileInfo.id;
    });

    file.description = fileInfo.description;
  },

  [types.SYS_FILE_UPLOAD_DELETE_SUC](state, {id}) {
    arrayUtil.removeItem(state.History, 'id', id);
  },

  [types.SYS_FILE_UPLOAD_QUERY_FILES_BEGIN](state) {
    state.History = [];
  },

  [types.SYS_FILE_UPLOAD_QUERY_FILES_SUC](state, {files}) {
    state.History = files || [];
  },
  [types.SYS_FILE_UPLOAD_QUERY_FILES_COUNT_BEGIN](state) {
    state.totalCount = 0;
  },

  [types.SYS_FILE_UPLOAD_QUERY_FILES_COUNT_SUC](state, {totalCount}) {
    state.totalCount = totalCount;
  },

  [types.SYS_FILE_DEL_HISTORY](state, {combinationId}) {
    console.log(state.History)
    console.log(combinationId)
    arrayUtil.removeItem(state.History, 'combinationId', combinationId);
  },

  [types.SYS_FILE_UPDATE_HISTORY](state, {combinationId, accumulatedIncome}) {
    for (let i = 0; i < state.History.length; i++) {
      let item = state.History[i];
      if (item.combinationId === combinationId) {
        item.accumulatedIncome = accumulatedIncome;
        break;
      }
    }
  }

};

export default mutations;
